﻿/*********************************************************************************
  *Copyright(C),2015-2020,
  *TUYA
  *www.tuya.comm

  *FileName: tuya_ipc_dp_handler.c
  *
  * File Description：
  * 1. DP Point Setting and Acquisition Function API
  *
  * Developer work：
  * 1. Local configuration acquisition and update.
  * 2. Set local IPC attributes, such as picture flip, time watermarking, etc.
  *    If the function is not supported, leave the function blank.
  *
**********************************************************************************/
#include "tuya_ipc_dp_handler.h"
#include "InSdkCallbackFunction.h"
#include "tuya_ipc_media_demo.h"
#include "tuya_ipc_sd_demo.h"
#include "tuya_iot_config.h"
#include "tuya_os_adapter.h"
#include "InSdkIniBasic.h"
#include "InSdkIniDev.h"
#include "InPerLed.h"

#define DEF_TAG         "TuyaDpHandle"

#ifdef TUYA_DP_SLEEP_MODE
Com_s32 g_sleep_mode = DEF_COM_FALSE;
VOID IPC_APP_set_sleep_mode(BOOL_T sleep_mode)
{
    /* sleep mode,BOOL type,true means sleep,false means wake */
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    udata.val = sleep_mode;
    Ext_InSdkIniIot_SetData(INI_DEV_INT_SLEEP_MODE,&udata);
    g_sleep_mode = sleep_mode;
}

BOOL_T IPC_APP_get_sleep_mode(VOID)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    Ext_InSdkIniIot_GetData(INI_DEV_INT_SLEEP_MODE,&udata);
    g_sleep_mode = udata.val;
    PRT_INFO(DEF_TAG,"get sleep mode: %d\n",g_sleep_mode);
    Ext_SdkMediaCom_SetSleepMode(g_sleep_mode);
    return g_sleep_mode;
}
#endif

#ifdef TUYA_DP_LIGHT
VOID IPC_APP_set_light_onoff(BOOL_T light_on_off)
{
    PRT_INFO(DEF_TAG,"set light: %d\n", light_on_off);
    /* Status indicator,BOOL type,true means on,false means off */
    Ext_InComFile_ChooseMode(DEF_DP_LIGHT_LED,light_on_off);
}

BOOL_T IPC_APP_get_light_onoff(VOID)
{
    BOOL_T light_on_off  = Ext_InComFile_ChooseMode(DEF_DP_LIGHT_LED,E_CHECK_FILE);
    PRT_INFO(DEF_TAG,"now light: %d\n", light_on_off);

    if(light_on_off == 1)
    {
        Ext_InPerLed_Set(LED_STATUS_ON);

    }
    else if (light_on_off == 0)
    {
        Ext_InPerLed_Set(LED_STATUS_OFF);
        Ext_SdkGpioWrite_WatchLed(DEF_COM_LEVEL_LOW);
    }

    return light_on_off;
}
#endif

#ifdef TUYA_DP_FLIP
VOID IPC_APP_set_flip_onoff(BOOL_T flip)
{
    PRT_INFO(DEF_TAG,"set flip: %d\n", flip);
    /* flip state,BOOL type,true means inverse,false means normal */
    UnInSdkIniCfgKeyData udata;

    memset(&udata,0,sizeof(udata));
    udata.val = flip;
    Ext_InSdkIniIot_SetData(INI_DEV_INT_VIDEO_FLIP,&udata);
}

BOOL_T IPC_APP_get_flip_onoff(VOID)
{
    UnInSdkIniCfgKeyData udata;

    memset(&udata, 0, sizeof(udata));
    Ext_InSdkIniIot_SetData(INI_DEV_INT_VIDEO_FLIP, &udata);
    BOOL_T flip = udata.val;
    PRT_INFO(DEF_TAG,"curr flip: %d\n", flip);
    int ret = Ext_SdkMediaCom_SetFlip(flip);
    if (ret == -1)
    {
        PRT_ERR(DEF_TAG,"set flip failed\n");
        return -1;
    }

    return ret;
}
#endif

#ifdef TUYA_DP_WATERMARK
VOID IPC_APP_set_watermark_onoff(BOOL_T watermark_on_off)
{
    PRT_INFO(DEF_TAG,"set watermark: %d\n", watermark_on_off);
    /* Video watermarking (OSD),BOOL type,true means adding watermark on,false means not */
    if (watermark_on_off == 0)
    {
        int ret = Ext_SdkOsd_Clear();
        if (ret == -1)
        {
            PRT_ERR(DEF_TAG,"clear osd error\n");
            return;
        }
    }

    UnInSdkIniCfgKeyData udata;

    memset(&udata,0,sizeof(udata));
    udata.val = watermark_on_off;
    Ext_InSdkIniIot_SetData(INI_DEV_INT_VIDEO_OSD,&udata);
}

BOOL_T IPC_APP_get_watermark_onoff(VOID)
{
    UnInSdkIniCfgKeyData udata;

    memset(&udata, 0, sizeof(udata));
    Ext_InSdkIniIot_SetData(INI_DEV_INT_VIDEO_OSD, &udata);
    BOOL_T watermark_on_off = udata.val;

    PRT_INFO(DEF_TAG,"get watermark: %d\n", watermark_on_off);
    Ext_SdkOsd_SetStatus(watermark_on_off);
    return watermark_on_off;
}
#endif

#ifdef TUYA_DP_NIGHT_MODE
STATIC CHAR_T s_night_mode[4] = {0}; // for demo
VOID IPC_APP_set_night_mode(PCHAR_T p_night_mode)
{
    /* Infrared night vision function,ENUM type*/
    // 0-automatic,1-off,2-on
    PRT_INFO(DEF_TAG, "set night mode:%s \n", p_night_mode);

    UnInSdkIniCfgKeyData udata;
    memset(&udata, 0, sizeof(udata));

    int current_mode = atoi(p_night_mode);
    udata.val = current_mode;
    Ext_InSdkIniIot_SetData(INI_DEV_INT_NIGHT_MODE, &udata);
}

CHAR_T *IPC_APP_get_night_mode(VOID)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata, 0, sizeof(udata));
    memset(s_night_mode,0,sizeof(s_night_mode));

    Ext_InSdkIniIot_GetData(INI_DEV_INT_NIGHT_MODE, &udata);

    switch (udata.val)
    {
    case NIGHT_MODE_AUTO:
        Ext_SdkNightMonitor_SetWorkMode(NIGHT_MODE_AUTO);
        break;

    case NIGHT_MODE_OFF:
        Ext_SdkNightMonitor_SetWorkMode(NIGHT_MODE_OFF);
        break;

    case NIGHT_MODE_ON:
        Ext_SdkNightMonitor_SetWorkMode(NIGHT_MODE_ON);
        break;

    default:
        break;
    }

    sprintf(s_night_mode,"%d",udata.val);
    PRT_INFO(DEF_TAG,"curr night mode:%s\n", s_night_mode);
    Ext_SdkNightMonitor_SwitchMode(DEF_COM_TRUE);

    return s_night_mode;
}
#endif

#ifdef TUYA_DP_ALARM_FUNCTION
VOID IPC_APP_set_alarm_function_onoff(BOOL_T alarm_on_off)
{
    PRT_INFO(DEF_TAG,"set md on off: %d\n", alarm_on_off);
    /* motion detection alarm switch,BOOL type,true means on,false means off.
     * This feature has been implemented, and developers can make local configuration settings and properties.*/
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    udata.val = alarm_on_off;
    Ext_InSdkIniIot_SetData(INI_DEV_INT_MD_STATUS,&udata);
}

BOOL_T IPC_APP_get_alarm_function_onoff(VOID)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    Ext_InSdkIniIot_GetData(INI_DEV_INT_MD_STATUS,&udata);
    PRT_INFO(DEF_TAG,"cur md on off: %d\n", udata.val);

    if (udata.val == 1)
    {
        Ext_TuyaMD_SetFlag(DEF_COM_TRUE);
    }
    else
    {
        Ext_TuyaMD_SetFlag(DEF_COM_FALSE);
    }
    return udata.val;
}
#endif

#ifdef TUYA_DP_ALARM_SENSITIVITY
STATIC CHAR_T s_alarm_sensitivity[4] = {0};//for demo
VOID IPC_APP_set_alarm_sensitivity(PCHAR_T p_sensitivity)
{
    /* Motion detection alarm sensitivity,ENUM type,0 means low sensitivity,1 means medium sensitivity,2 means high sensitivity */
    UnInSdkIniCfgKeyData udata;

    memset(&udata,0,sizeof(udata));

    Com_s32 md_sen = atoi(p_sensitivity);
    udata.val = md_sen;
    Ext_InSdkIniIot_SetData(INI_DEV_INT_MD_SENSITIVITY,&udata);
}

CHAR_T *IPC_APP_get_alarm_sensitivity(VOID)
{
    UnInSdkIniCfgKeyData udata;

    memset(&udata,0,sizeof(udata));
    memset(s_alarm_sensitivity,0,sizeof(s_alarm_sensitivity));

    Ext_InSdkIniIot_GetData(INI_DEV_INT_MD_SENSITIVITY,&udata);
    Ext_TuyaMD_SetSensitivity(udata.val);
    sprintf(s_alarm_sensitivity,"%d",udata.val);
    PRT_INFO(DEF_TAG,"alarm sensitivity: %s\n", s_alarm_sensitivity);

    return s_alarm_sensitivity;
}
#endif

#ifdef TUYA_DP_MOTION_TIMER_SWITCH
VOID IPC_APP_set_motion_timer_switch(BOOL_T val)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    udata.val = val;
    Ext_InSdkIniIot_SetData(INI_DEV_INT_MD_TIME_SWITCH,&udata);
}

BOOL_T IPC_APP_get_motion_timer_switch(VOID)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    Ext_InSdkIniIot_GetData(INI_DEV_INT_MD_TIME_SWITCH,&udata);
    BOOL_T val = udata.val;
    return val;
}
#endif

#ifdef TUYA_DP_MOTION_TIMER_SETTING
VOID IPC_APP_set_motion_timer_setting(PCHAR_T time)
{
    Ext_InSdkIniIot_SetStr(INI_DEV_STR_MOTION_TIME,time);
}

VOID IPC_APP_get_motion_timer_setting(PCHAR_T time)
{
    Ext_InSdkIniIot_GetStr(INI_DEV_STR_MOTION_TIME,time);
}
#endif

#ifdef TUYA_DP_ALARM_ZONE_ENABLE
VOID IPC_APP_set_alarm_zone_onoff(BOOL_T alarm_zone_on_off)
{
    /* Motion detection area setting switch,BOOL type,true means on,false is off*/
    PRT_INFO(DEF_TAG,"set alarm zone: %d\n", alarm_zone_on_off);

    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    udata.val = alarm_zone_on_off;
    /* This feature has been implemented, and developers can make local configuration settings and properties.*/
    Ext_InSdkIniIot_SetData(INI_DEV_INT_ALARM_ZONE,&udata);
}

BOOL_T IPC_APP_get_alarm_zone_onoff(VOID)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    Ext_InSdkIniIot_GetData(INI_DEV_INT_ALARM_ZONE,&udata);
    Com_s32 val = udata.val;
    PRT_DBG(DEF_TAG,"alarm zone: %d\n", val);
    return val;
}
#endif

#ifdef TUYA_DP_ALARM_ZONE_DRAW
static char s_alarm_zone[256] = {0};
VOID IPC_APP_set_alarm_zone_draw(PCHAR_T p_alarm_zone)
{
    if (NULL == p_alarm_zone)
    {
        PRT_ERR(DEF_TAG,"alarm zone is NULL\n");
        return;
    }

    Ext_InSdkIniIot_SetStr(INI_DEV_STR_ALARM_ZONE_DRAW,p_alarm_zone);
    return;
}

char *IPC_APP_get_alarm_zone_draw(VOID)
{
    memset(s_alarm_zone,0,sizeof(s_alarm_zone));

    Ext_InSdkIniIot_GetStr(INI_DEV_STR_ALARM_ZONE_DRAW,s_alarm_zone);
    if(strstr(s_alarm_zone,"off") == NULL)
    {
        Ext_SdkMotionDetectArea_ParsePointsZone(s_alarm_zone);
        return s_alarm_zone;
    }
    else
    {
        memset(s_alarm_zone,0,sizeof(s_alarm_zone));
        return s_alarm_zone;
    }
}
#endif

#ifdef TUYA_DP_ALARM_INTERVAL
STATIC CHAR_T s_alarm_interval[4];//for demo
VOID IPC_APP_set_alarm_interval(PCHAR_T p_interval)
{
    PRT_INFO(DEF_TAG,"set interval:%s \n", p_interval);
    /* Motion detection alarm interval,unit is minutes,ENUM type,"1","3","5" */
    Com_s32 val = atoi(p_interval);

    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    udata.val = val;
    Ext_InSdkIniIot_SetData(INI_IOT_MD_INTERVAL,&udata);
}

CHAR_T *IPC_APP_get_alarm_interval(VOID)
{
    int val = 0;
    /* Motion detection alarm interval,unit is minutes,ENUM type,"1","3","5" */
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    Ext_InSdkIniIot_GetData(INI_IOT_MD_INTERVAL,&udata);
    val = udata.val;
    Ext_TuyaMD_SetInterval(val);

    memset(s_alarm_interval,0,sizeof(s_alarm_interval));
    switch(val)
    {
    case 1 :
        sprintf(s_alarm_interval,"%d",0);
        break;

    case 3 :
        sprintf(s_alarm_interval,"%d",1);
        break;

    case 5 :
        sprintf(s_alarm_interval,"%d",2);
        break;

    default:
        break;
    }
    printf("curr alarm_intervaly:%s \r\n", s_alarm_interval);

    return s_alarm_interval;
}
#endif

#ifdef TUYA_DP_SD_STATUS_ONLY_GET
extern E_SD_STATUS g_sd_status;
INT_T g_sd_formatting = DEF_COM_FALSE;

INT_T IPC_APP_get_sd_status(VOID)
{
    INT_T sd_status;
    /* SD card status, VALUE type, 1-normal, 2-anomaly, 3-insufficient space, 4-formatting, 5-no SD card */
    /* Developer needs to return local SD card status */
    if(g_sd_formatting == DEF_COM_TRUE)
    {
        sd_status = SD_STATUS_FORMATING;
    }
    else
    {
        sd_status = g_sd_status;
    }

    PRT_DBG(DEF_TAG,"curr sd_status: %d\n", sd_status);
    return sd_status;
}
#endif

#ifdef TUYA_DP_SD_STORAGE_ONLY_GET
extern StructSDStorage g_struct_sd_storage;
VOID IPC_APP_get_sd_storage(UINT_T *p_total, UINT_T *p_used, UINT_T *p_empty)
{//unit is kb
    /* Developer needs to return local SD card status */
    *p_total = g_struct_sd_storage.total;
    *p_used  = g_struct_sd_storage.used;
    *p_empty = *p_total - *p_used;

    PRT_INFO(DEF_TAG,"sd total:%d used:%d empty:%d\n",*p_total, *p_used, *p_empty);
}
#endif

#ifdef TUYA_DP_SD_RECORD_ENABLE
VOID IPC_APP_set_sd_record_onoff(BOOL_T sd_record_on_off)
{
    //PRT_INFO(DEF_TAG,"set sd record: %d\n", sd_record_on_off);
    /* SD card recording function swith, BOOL type, true means on, false means off.
     * This function has been implemented, and developers can make local configuration settings and properties.*/
    if(sd_record_on_off == TRUE)
    {
        Ext_InComFile_ChooseMode(DEF_DP_ENABLE_SD_RECORD,E_TOUCH_FILE);
        IPC_APP_set_sd_record_mode(IPC_APP_get_sd_record_mode());
    }
    else
    {
        Ext_InComFile_ChooseMode(DEF_DP_ENABLE_SD_RECORD,E_REMOVE_FILE);
        tuya_ipc_ss_set_write_mode(SS_WRITE_MODE_NONE);
    }
}

BOOL_T IPC_APP_get_sd_record_onoff(VOID)
{
    BOOL_T sd_record_on_off = Ext_InComFile_ChooseMode(DEF_DP_ENABLE_SD_RECORD,E_CHECK_FILE);
    // PRT_INFO(DEF_TAG,"sd record status: %d\n", sd_record_on_off);

    return sd_record_on_off;
}
#endif

#ifdef TUYA_DP_SD_RECORD_MODE
typedef enum
{
    RECORD_MODE_EVENT = 0,
    RECORD_MODE_ALLTIME,
}ETyRecordMode;

VOID IPC_APP_set_sd_record_mode(UINT_T sd_record_mode)
{
    PRT_INFO(DEF_TAG,"set record mode: %d\n", sd_record_mode);

    if(sd_record_mode == RECORD_MODE_EVENT)
    {
        tuya_ipc_ss_set_write_mode(SS_WRITE_MODE_EVENT);
        Ext_InComFile_ChooseMode(DEF_DP_EVENT_RECORD,E_TOUCH_FILE);
    }
    else
    {
        tuya_ipc_ss_set_write_mode(SS_WRITE_MODE_ALL);
        Ext_InComFile_ChooseMode(DEF_DP_EVENT_RECORD,E_REMOVE_FILE);
    }
}

UINT_T IPC_APP_get_sd_record_mode(VOID)
{
    UINT_T sd_record_mode = Ext_InComFile_ChooseMode(DEF_DP_EVENT_RECORD,E_CHECK_FILE);
    sd_record_mode = (sd_record_mode == 1) ? RECORD_MODE_EVENT : RECORD_MODE_ALLTIME;

    return sd_record_mode;
}
#endif

#ifdef TUYA_DP_SD_UMOUNT
BOOL_T IPC_APP_unmount_sd_card(VOID)
{
    /* unmount sdcard */
    BOOL_T umount_ok = tuya_ipc_sd_umount();
    PRT_INFO(DEF_TAG,"unmount result: %d\n", umount_ok);
    return umount_ok;
}
#endif

#ifdef TUYA_DP_SD_FORMAT
/* -2000: SD card is being formatted, -2001: SD card formatting is abnormal, -2002: No SD card, 
   -2003: SD card error. Positive number is formatting progress */
typedef enum
{
    SD_FORMATTING       = -2000,
    SD_FORMAT_ERROR     = -2001,
    SD_NO_INSERT        = -2002,
    SD_STATUS_ERROR     = -2003,
}EnumSDFormatStatus;

extern E_SD_STATUS g_sd_status;
STATIC INT_T s_sd_format_progress = 0;
Com_s8 g_start_sdcard_format = DEF_COM_FALSE;
void *thread_sd_format(void *arg)
{
    g_sd_formatting = DEF_COM_TRUE;
    /* First notify to app, progress 0% */
    s_sd_format_progress = 1;
    IPC_APP_report_sd_format_status(s_sd_format_progress);
    sleep(1);
    g_start_sdcard_format = DEF_COM_TRUE;

    tuya_ipc_ss_set_write_mode(SS_WRITE_MODE_NONE);
    tuya_ipc_ss_pb_stop_all();

    /* Perform SD card formatting operation */
    if(tuya_ipc_sd_format() == FALSE)
    {
        IPC_APP_report_sd_format_status(SD_FORMAT_ERROR);
        PRT_ERR(DEF_TAG,"sd format error\n");
        return NULL;
    }

    UINT_T a,b,c;
    tuya_ipc_sd_get_capacity(&a,&b,&c);
    IPC_APP_report_sd_storage();

    sleep(1);
    g_start_sdcard_format = DEF_COM_FALSE;
    /* progress 100% */
    s_sd_format_progress = 100;
    IPC_APP_report_sd_format_status(s_sd_format_progress);

    g_sd_formatting = DEF_COM_FALSE;
    return NULL;
}

VOID IPC_APP_format_sd_card(VOID)
{
    PRT_INFO(DEF_TAG,"start to format sd_card\n");
    /* SD card formatting.
     * The SDK has already completed the writing of some of the code,
     and the developer only needs to implement the formatting operation. */

    if(g_sd_status == SD_STATUS_NOT_EXIST)
    {
        IPC_APP_report_sd_format_status(SD_NO_INSERT);
        return;
    }

    if(g_sd_formatting == DEF_COM_FALSE)
    {
        pthread_t sd_format_thread;
        pthread_create(&sd_format_thread, NULL, thread_sd_format, NULL);
        pthread_detach(sd_format_thread);
    }
    else
    {
        IPC_APP_report_sd_format_status(SD_FORMATTING);
    }
}
#endif

#ifdef TUYA_DP_SD_FORMAT_STATUS_ONLY_GET
INT_T IPC_APP_get_sd_format_status(VOID)
{
    if((g_start_sdcard_format == DEF_COM_TRUE) &&
       (s_sd_format_progress < 99))
    {
        s_sd_format_progress++;
    }

    return s_sd_format_progress;
}
#endif

#ifdef TUYA_DP_PTZ_CONTROL
VOID IPC_APP_ptz_start_move(CHAR_T *p_direction)
{
    PRT_INFO(DEF_TAG,"ptz move: %s\n", p_direction);
    int direction = atoi(p_direction);

    switch(direction)
    {
    //0-up, 4-down, 6-left, 2-right, 1-upper right, 3-lower right, 5-down left, 7-top left
    case 0 :
    {
#if CMAKE_SUPPORT_PTZ
        Ext_InPerStepMotor_Up();
#endif

#if CMAKE_SUPPORT_STEEP_ENGINE
        Ext_InPerSteerEngine_TurnLeft();
#endif
    }
        break;

    case 4 :
    {
#if CMAKE_SUPPORT_PTZ
        Ext_InPerStepMotor_Down();
#endif

#if CMAKE_SUPPORT_STEEP_ENGINE
        Ext_InPerSteerEngine_TurnRight();
#endif
    }
        break;

    case 6 :
    {
#if CMAKE_SUPPORT_STEP_MOTOR
        Ext_InPerStepMotor_TurnLeft();
#endif
    }
        break;

    case 2 :
    {
#if CMAKE_SUPPORT_STEP_MOTOR
        Ext_InPerStepMotor_TurnRight();
#endif
    }
        break;

    default:
        break;
    }
}
#endif

#ifdef TUYA_DP_PTZ_STOP
VOID IPC_APP_ptz_stop_move(VOID)
{
    printf("ptz stop move \r\n");
    /* PTZ rotation stops */
}
#endif

#ifdef TUYA_DP_PTZ_CHECK
void IPC_APP_ptz_check(VOID)
{
    printf("ptz check \r\n");
}
#endif

#ifdef TUYA_DP_TRACK_ENABLE
void IPC_APP_track_enable(BOOL_T track_enable)
{
    printf("track_enable %d\r\n",track_enable);
}

BOOL_T IPC_APP_get_track_enable(void)
{
    char track_enable = 0;
    //the value you get yourself
    return (BOOL_T)track_enable;
}

#endif

#ifdef TUYA_DP_IPC_CONTRAST
VOID IPC_APP_set_ipc_contrast(INT_T val)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    udata.val = val;
    Ext_InSdkIniIot_SetData(INI_DEV_INT_IPC_CONTRAST,&udata);
}

INT_T IPC_APP_get_ipc_contrast(VOID)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));

    Ext_InSdkIniIot_GetData(INI_DEV_INT_IPC_CONTRAST,&udata);
    Ext_SdkMediaCom_SetVpssEffect(VIDEO_EFFECT_CONTRAST,udata.val);

    return udata.val;
}
#endif

#ifdef TUYA_DP_IPC_BRIGHT
VOID IPC_APP_set_ipc_bright(INT_T val)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    udata.val = val;
    Ext_InSdkIniIot_SetData(INI_DEV_INT_IPC_BRIGHT,&udata);
}

INT_T IPC_APP_get_ipc_bright(VOID)
{
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));

    Ext_InSdkIniIot_GetData(INI_DEV_INT_IPC_BRIGHT,&udata);
    Ext_SdkMediaCom_SetVpssEffect(VIDEO_EFFECT_BRIGHTNESS,udata.val);

    return udata.val;
}
#endif

#ifdef TUYA_DP_SET_SPK_VOLUME
#define DEV_TO_APP  0
#define APP_TO_DEV  1
Com_s32 volume_val_transform(Com_s32 volume,Com_s32 mode)
{
    Com_s32 val = 0;

    if(APP_TO_DEV == mode)
    {
        val = ((40 + volume) / 4) + 1;
    }

    if(DEV_TO_APP == mode)
    {
        val = (volume - 1) * 4 - 40;
    }

    return val;
}

VOID IPC_APP_set_spk_volume(INT_T val)
{
    PRT_NORMAL("set volume: %d\n", val);
    Com_s32 s_val;

    s_val = volume_val_transform(val,APP_TO_DEV);
    Ext_InSdkIniIot_SetData(INI_DEV_INT_SPK_VOLUME,s_val);
}

INT_T IPC_APP_get_spk_volume(VOID)
{
    Com_s32 volume = 0;
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    Ext_InSdkIniIot_GetData(INI_DEV_INT_SPK_VOLUME,&udata);
    volume = udata.val;
    Ext_SdkMediaCom_SetSpkVolume(volume);
    volume = volume_val_transform(volume,DEV_TO_APP);
    return volume;
}
#endif

#ifdef TUYA_DP_AP_MODE
STATIC CHAR_T response[128] = {0};
#define WIFI_SSID_LEN 32    // tuya sdk definition WIFI SSID MAX LEN
#define WIFI_PASSWD_LEN 64  // tuya sdk definition WIFI PASSWD MAX LEN
extern OPERATE_RET tuya_adapter_wifi_get_mac(IN CONST WF_IF_E wf,INOUT NW_MAC_S *mac);
CHAR_T * IPC_APP_get_ap_mode(VOID)
{
    CHAR_T ap_ssid[WIFI_SSID_LEN + 1] = {0};
    CHAR_T ap_pw[WIFI_PASSWD_LEN + 1] = {0};
    WF_WK_MD_E cur_mode = WWM_STATION;
    NW_MAC_S mac = {{0}};
    INT_T is_ap = 0;

    OPERATE_RET op_ret = tuya_adapter_wifi_get_mac(WF_AP,&mac);
    if(OPRT_OK != op_ret)
    {
        strncpy(ap_ssid, "TUYA_IPC_AP", WIFI_SSID_LEN);
    }
    else
    {
        snprintf(ap_ssid, WIFI_SSID_LEN, "TUYA_IPC-%02X%02X",mac.mac[4],mac.mac[5]);
    }

    if(cur_mode == WWM_SOFTAP)
    {
        is_ap = 1;
    }

    snprintf(response, 128, "{\\\"is_ap\\\":%d,\\\"ap_ssid\\\":\\\"%s\\\",\\\"password\\\":\\\"%s\\\"}",is_ap,ap_ssid,ap_pw);
    return response;
}

#ifdef TUYA_DP_AP_SWITCH
VOID* __ap_change_thread(void *arg)
{
    return NULL;
}

VOID change_ap_process()
{
    pthread_t ap_change_thread;
    int ret = pthread_create(&ap_change_thread, NULL, __ap_change_thread, NULL);
    if(ret != 0)
    {
        printf("ap_change_thread ,create fail! ret:%d\n",ret);
    }

    pthread_detach(ap_change_thread);
}

INT_T IPC_APP_set_ap_mode(IN cJSON *p_ap_info)
{
    if (NULL == p_ap_info)
    {
        return 0;
    }

    INT_T ap_onoff = -1;
    return ap_onoff;
}
#endif

#endif

#ifdef TUYA_DP_RGB_LED_COLOUR
VOID IPC_APP_set_rgb_led_colour(INT_T val)
{
    Ext_InSdkIniIot_SetData(INI_IOT_RGB_LED_COLOUR,val);
}

INT_T IPC_APP_get_rgb_led_colour(VOID)
{
    Com_s32 val;
    UnInSdkIniCfgKeyData udata;
    memset(&udata,0,sizeof(udata));
    Ext_InSdkIniIot_GetData(INI_IOT_RGB_LED_COLOUR,&udata);
    val = udata.val;
    PRT_NORMAL("rgb led: %d\n",val);
    return val;
}
#endif

VOID IPC_APP_set_bool_switch(TY_OBJ_DP_S *p_obj_dp)
{
    PRT_INFO(DEF_TAG,"recv set_bool_switch:DP[%d]: %d\n",p_obj_dp->dpid, p_obj_dp->value.dp_bool);
    Ext_InPerUartSend_BoolSW(p_obj_dp->dpid, p_obj_dp->value.dp_bool);
}
